package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolderThreadLocal;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/11/29 17:24
 */

@RequestMapping("/users")
@RestController
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询用户详细信息
     * @param userID
     * @return
     */
    @GetMapping
    public ResponseEntity userInfo(Long userID){
        // 检查token
       /* boolean verifyToken = JwtUtils.verifyToken(token);
        if(!verifyToken){
            return ResponseEntity.status(401).body(null);
        }
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");*/

        Long id = UserHolderThreadLocal.getId();
        // 前端的id是不一定存在的
        if(userID == null){
            userID = id;
        }
        UserInfoVo userInfoVo  = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 用户资料更新
     * @param userInfo
     * @return
     */
    @PutMapping
    public ResponseEntity update(@RequestBody UserInfo userInfo){

      /*  boolean verifyToken = JwtUtils.verifyToken(token);
        if(!verifyToken){
            return ResponseEntity.status(401).body(null);
        }
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");*/
        Long id = UserHolderThreadLocal.getId();
        // 调用service
        userInfoService.updateInfo(id, userInfo);
        return ResponseEntity.ok(null);
    }



}
